package io.renren.modules.se.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.se.dao.EquipmentDao;
import io.renren.modules.se.dto.EquipmentDTO;
import io.renren.modules.se.entity.EquipmentEntity;
import io.renren.modules.se.service.EquipmentService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.enums.SuperAdminEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 器材信息表
 *
 * 
 * 
 */
@Service
public class EquipmentServiceImpl extends CrudServiceImpl<EquipmentDao, EquipmentEntity, EquipmentDTO> implements EquipmentService {

    @Autowired
    private EquipmentDao equipmentDao;


    @Override
    public QueryWrapper<EquipmentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<EquipmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    //比较出借数量与库存数量
    @Override
    public boolean compareQuantity(Integer outNum) {
        List<Map<String, Object>> inventory = equipmentDao.selectMaps(new QueryWrapper<EquipmentEntity>().lt("inventory", outNum));
        //查出数据就代表输入的借出数量比库存数量大
        return !inventory.isEmpty();
    }

    //更新库存数量
    @Override
    public void alterInventory(String eid, Integer changeNnumber) {
        EquipmentEntity equipmentEntity = equipmentDao.selectById(eid);
        equipmentEntity.setInventory(changeNnumber);
        equipmentDao.updateById(equipmentEntity);
    }

    @Override
    public void changeENumber(String eid, Integer inNum) {
        EquipmentEntity entity = equipmentDao.selectOne(new QueryWrapper<EquipmentEntity>().eq("id", eid));
        entity.setInventory(entity.getInventory()+inNum);
        equipmentDao.updateById(entity);
    }


}